///**
// * Copyright 2016 Bartosz Schiller
// *
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//package com.ashlikun.pdfview
//
//import android.graphics.Bitmap
//import android.os.Handler
//import android.os.Looper
//import android.os.Message
//import android.util.Log
//import com.ashlikun.pdfview.RenderingHandler.RenderingTask
//import com.ashlikun.pdfview.exception.PageRenderingException
//import com.ashlikun.pdfview.model.PagePart
//import com.ashlikun.pdfview.util.Constants
//import com.ashlikun.pdfview.util.PdfViewUtil
//import kotlin.math.roundToInt
//
///**
// * 将处理传入的[RenderingTask]消息的[Handler]
// * 当的部分
// * PDF已准备好渲染.
// */
//internal class RenderingHandler(looper: Looper, private val pdfView: PDFView) : Handler(looper) {
//    private var running = false
//    private val loadings = ArrayList<Int>()
//    fun addRenderingTask(page: Int, width: Float, height: Float, bestQuality: Boolean, annotationRendering: Boolean) {
//        val task = RenderingTask(width, height, page, bestQuality, annotationRendering)
//        val msg = obtainMessage(MSG_RENDER_TASK, task)
//        sendMessage(msg)
//    }
//
//    override fun handleMessage(message: Message) {
//        val task = message.obj as RenderingTask
//        try {
//            if (running) {
//                val part = proceed(task)
//                if (part != null) {
//                    if (running) {
//                        pdfView.post {
//
//                            //内部添加进缓存了
//                            pdfView.onBitmapRendered(part)
//                            //清除加载中,线程安全
//                            synchronized(loadings) { loadings.remove(task.page as Any) }
//                        }
//                    } else {
//                        part.bitmap.recycle()
//                    }
//                }
//            }
//        } catch (ex: PageRenderingException) {
//            pdfView.post { pdfView.onPageError(ex) }
//        }
//    }
//
//    @Throws(PageRenderingException::class)
//    private fun proceed(renderingTask: RenderingTask): PagePart? {
//        synchronized(loadings) {
//            if (loadings.contains(renderingTask.page)) return null
//            loadings.add(renderingTask.page)
//        }
//        val pdfFile = pdfView.pdfFile
//        pdfFile!!.openPage(renderingTask.page)
//        val w = renderingTask.width.roundToInt()
//        val h = renderingTask.height.roundToInt()
//        if (w == 0 || h == 0 || pdfFile.pageHasError(renderingTask.page)) {
//            return null
//        }
//        val render: Bitmap
//        try {
//            if (Constants.DEBUG_MODE) PdfViewUtil.logi("proceed start")
//            render = if (pdfFile.isSystemRenderer) {
//                Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
//            } else {
//                Bitmap.createBitmap(w, h, if (renderingTask.bestQuality) Bitmap.Config.ARGB_8888 else Bitmap.Config.RGB_565)
//            }
//            if (Constants.DEBUG_MODE) PdfViewUtil.logi("proceed end")
//        } catch (e: IllegalArgumentException) {
//            Log.e(TAG, "Cannot create bitmap", e)
//            return null
//        }
//        pdfFile.renderPageBitmap(render, renderingTask.page, w, h, renderingTask.annotationRendering)
//        return PagePart(renderingTask.page, render)
//    }
//
//    fun stop() {
//        running = false
//    }
//
//    fun start() {
//        loadings.clear()
//        running = true
//    }
//
//    private inner class RenderingTask internal constructor(var width: Float, var height: Float, var page: Int, var bestQuality: Boolean, var annotationRendering: Boolean)
//    companion object {
//        /**
//         * [Message.what] 此处理程序处理的消息类型。
//         */
//        const val MSG_RENDER_TASK = 1
//        private val TAG = RenderingHandler::class.java.name
//    }
//}
